package com.songcan.oauth.service.impl;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.songcan.common.exception.ObjectNotException;
import com.songcan.common.utils.HashPasswordService;
import com.songcan.model.oauth.SysDeptUser;
import com.songcan.model.oauth.SysUser;
import com.songcan.model.oauth.SysUserRole;
import com.songcan.oauth.mapper.SysUserMapper;
import com.songcan.oauth.service.SysDeptUserService;
import com.songcan.oauth.service.SysUserRoleService;
import com.songcan.oauth.service.SysUserService;
import com.songcan.oauth.vo.UserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Objects;

@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {
    private HashPasswordService hashPasswordService;
    private SysUserRoleService sysUserRoleService;
    private SysDeptUserService sysDeptUserService;

    @Autowired
    public SysUserServiceImpl(HashPasswordService hashPasswordService,
                               SysUserRoleService sysUserRoleService,
                               SysDeptUserService sysDeptUserService){

        this.hashPasswordService = hashPasswordService;
        this.sysUserRoleService = sysUserRoleService;
        this.sysDeptUserService = sysDeptUserService;
    }

    @Override
    public UserVO selectByDetail(Long userId) {
        return baseMapper.selectByDetail(userId);
    }

    @Override
    public List<String> selectByDeptRole(Long userId) {
        return baseMapper.selectByDeptRole(userId);
    }


    @Override
    public IPage<UserVO> selectByUserPage(Page page, String keyWord) {
        return baseMapper.selectByUserPage(page,keyWord);
    }

    @Override
    public void addUser(String userName, String password, Long deptId, String phone, Integer sex, String avatar, Long roleId,
                        String remark,Date birthDay,String name,Integer locked,Integer enable,Long positionId) {
        com.songcan.common.vo.UserVO userVO = new com.songcan.common.vo.UserVO();
        userVO.setPassword(password);
        hashPasswordService.hashAuthentication(userVO);
        SysUser sysUser = new SysUser(IdWorker.getId(),userName,userVO.getPassword(),userVO.getSalt(),phone,avatar,new Date(),
                null,locked,0,enable,remark,birthDay,sex,null,name,0);
        save(sysUser);
        SysUserRole sysUserRole = new SysUserRole(sysUser.getUserId(),roleId);
        sysUserRoleService.save(sysUserRole);
        SysDeptUser sysDeptUser = new SysDeptUser(deptId,sysUser.getUserId(),roleId,positionId);
        sysDeptUserService.save(sysDeptUser);
    }

    @Override
    public void updateUser(Long userId,String userName, String password, Long deptId, String phone, Integer sex, String avatar, Long roleId, String remark, Date birthDay, String name,
                           Integer locked,Integer enable,Long positionId) {
        SysUser sysUser = getById(userId);
        if(Objects.isNull(sysUser)){
            throw new ObjectNotException("用户不存在");
        }

        if(Objects.nonNull(password)&& StrUtil.isNotEmpty(password)){
            com.songcan.common.vo.UserVO userVO = new com.songcan.common.vo.UserVO();
            userVO.setPassword(password);
            hashPasswordService.hashAuthentication(userVO);
            sysUser.setPassword(password);
            sysUser.setSalt(userVO.getSalt());
        }
        sysUser.setUsername(userName);
        sysUser.setPhone(phone);
        sysUser.setSex(sex);
        sysUser.setAvatar(avatar);
        sysUser.setRemark(remark);
        sysUser.setBirthDay(birthDay);
        sysUser.setName(name);
        sysUser.setLockFlag(locked);
        sysUser.setEnabled(enable);
        saveOrUpdate(sysUser);
        if(Objects.nonNull(roleId)){
            SysUserRole sysUserRole = sysUserRoleService.getOne(Wrappers.lambdaQuery(new SysUserRole()).eq(SysUserRole::getUserId,sysUser.getUserId()));
            if(Objects.isNull(sysUserRole)){
                sysUserRole = new SysUserRole(sysUser.getUserId(),roleId);
            }

            sysUserRoleService.saveOrUpdate(sysUserRole);
        }
        if(Objects.nonNull(deptId)){
            SysDeptUser sysDeptUser = sysDeptUserService.getOne(Wrappers.lambdaQuery(new SysDeptUser()).eq(SysDeptUser::getUserId,sysUser.getUserId()));
            if(Objects.nonNull(sysDeptUser)){
                sysDeptUserService.removeById(sysDeptUser.getDeptId());
            }

            sysDeptUser = new SysDeptUser(deptId,userId,roleId,positionId);

            sysDeptUserService.saveOrUpdate(sysDeptUser);
        }
        sysUser.setUpdateTime(new Date());
        update(sysUser,Wrappers.lambdaUpdate(new SysUser()).eq(SysUser::getUserId,userId));
    }

    @Override
    public void deleteByUser(Long userId) {
        sysUserRoleService.remove(Wrappers.lambdaQuery(new SysUserRole()).eq(SysUserRole::getUserId,userId));
        sysDeptUserService.remove(Wrappers.lambdaQuery(new SysDeptUser()).eq(SysDeptUser::getUserId,userId));
        removeById(userId);
    }
}
